﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HCI.Questionaire.Controls
{

    [TemplateVisualState(Name = StateDisabled, GroupName = GroupCommon)]
    [TemplatePart(Name = IndicatorsElement, Type = typeof(TextBlock))]
    public class ValueSequenceRatingControl : ValueRatingControl
    {

        public ValueSequenceRatingControl():base()
        {

        }
        internal override void UpdateChecked(ValueRatingButton b)
        {
            this.Total++;
            //if (this.Score == 0)
            //    this.Score = b.Rating;
            //else
            //    this.Score = ((this.Score + b.Rating) / 2);

            this.Score = b.Rating;

            int index = 0;
            while (index < _ratingElements.Count)
            {
                ValueRatingButton target = _ratingElements[index].Target as ValueRatingButton;
                if (target != null)
                {
                    target.IsChecked = CalculateIsChecked(target.Rating, this.Score);
                    index++;
                }
            }
        }

        internal override void UpdateState(ValueRatingButton b, string state)
        {
            try
            {
                int index = 0;
                while (index < _ratingElements.Count)
                {
                    ValueRatingButton target = _ratingElements[index].Target as ValueRatingButton;
                    if (target != null)
                    {
                        if ((target == b))
                        {
                            if (state.Equals(ValueRatingButton.StateMouseOver) && UseIndicators)
                            {
                                if (b.Text != null)
                                    _indicatorsElement.Text = b.Text;
                            }
                            else
                                UpdateLabel();
                            VisualStateManager.GoToState(target, state, true);
                            return;
                        }
                        else
                            VisualStateManager.GoToState(target, state, true);
                        index++;
                    }
                }
            }
            catch { }
        }

        internal override void PopulateControl(bool existing)
        {
            for (int i = 0; i < this.Max; i++)
            {
                ValueRatingButton button;
                if (existing)
                    button = _panelElement.Children[i] as ValueRatingButton;
                else
                {
                    button = new ValueRatingButton();
                    _panelElement.Children.Add(button);
                }

                button.Rating = i + 1;
                button.RatingControl = this;
                button.IsChecked = CalculateIsChecked(button.Rating, this.Score);

                button.Click += new RoutedEventHandler(button_Click);
                _ratingElements.Add(new WeakReference(button));
            }

        }
    }
}
